<?php
/**
 * image file class
 *
 * uses GD extension
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package webappkit
 * @subpackage fs
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * @property int $width width in pixels
 * @property int $height height in pixels
 * @property int $type GD types constant
 * @property string $mime mime type
 * @property int $bits
 * @property int $channels
 */
class WakImageFile extends WakFile {


    function WakImageFile($path) {
        parent::WakFile($path);
        $this->_setProperties();
    }

    /**
     * create a thumbnail of the image and return it.
     *
     * should keep type and ratio
     *
     * @param string $path thumbnail path
     * @param int $max_size maximum size for biggest dimension (width or height)
     * @return WakImageFile
     */
    function getThumbnail($path, $max_size) {
        // defining new size
        switch (true) {
            case $this->width <= $max_size and $this->height <= $max_size:
            // resizing is not necessary
                return $this->getCopy($path);
            case $this->width >= $this->height: // paysage
                // $ratio = $width / $max;
                $width = $max_size;
                $height = round($this->height * ($max_size /$this->width));
                break;
            case $this->height >= $this->width: // paysage
                // $ratio = $width / $max;
                $height = $max_size;
                $width = round($this->width * ($max_size / $this->height));
                break;
        }
        // getting image resources
        $self = $this->_getImg();
        // imagecreate may alter image but imagecreatetruecolor may not bea available
        if (function_exists('imagecreatetruecolor')) {
            $thumb = imagecreatetruecolor($width, $height);
        } else {
            $thumb = imagecreate($width, $height);
        }
        // creating thumbnail
        if (!imagecopyresized(
        $thumb, $self
        , 0, 0, 0, 0
        , $width, $height, $this->width, $this->height)) {
            trigger_error("failed to resize");
            return false;
        }
        switch ($this->type) {
            case IMAGETYPE_JPEG:
                $ok = imagejpeg($thumb, $path, 100);
                break;
            case IMAGETYPE_PNG:
                $ok = imagepng($thumb, $path);
                break;
            case IMAGETYPE_GIF:
                $ok = imagegif($thumb, $path);
                break;
        }
        if (!$ok)
            return false;
        return new WakImageFile($path);
    }

    /**
     * @return int
     * /
    function getWidth() {
        list($width, $height) = getimagesize($this->realpath());
        return $width;
    }/**/

    /**
     * @return int
     * /
    function getHeight() {
        list($width, $height) = getimagesize($this->realpath());
        return $height;
    }/**/

    /**
     * sets properties from file
     * @return bool
     */
    function _setProperties() {
        if (!$this->exists())
            return false;
        if (!$info = getimagesize($this->realPath(), $this->info))
            return false;
        $this->width = $info[0];
        $this->height = $info[1];
        $this->type = $info[2];
        $this->mime = $info['mime'];
        $this->bits = $info['bits'];
        if (isset($info['channels']))
            $this->channels = $info['channels'];
        return true;
    }


    /**
     * get image resource
     * @return resource as from imagecreatefromjpeg etc
     */
    function _getImg() {
        switch ($this->type) {
            case IMAGETYPE_JPEG:
                return imagecreatefromjpeg($this->realpath());
            case IMAGETYPE_PNG:
                return imagecreatefrompng($this->realpath());
            case IMAGETYPE_GIF:
                return imagecreatefromgif($this->realpath());
            default:
                trigger_error("failed to create image resource, unknown type ".$this->type);
                return false;
        }
    }
}